package com.cy.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cy.study.common.BasePageReq;
import com.cy.study.entity.FileEntity;
import com.cy.study.entity.UserInfoEntity;
import com.cy.study.exception.BusinessException;
import com.cy.study.mapper.FileMapper;
import com.cy.study.mapper.UserInfoMapper;
import com.cy.study.req.*;
import com.cy.study.res.*;
import com.cy.study.service.FileService;
import com.cy.study.service.UserInfoService;
import com.cy.study.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
@RequiredArgsConstructor
public class FileServiceImpl extends ServiceImpl<FileMapper, FileEntity> implements FileService {

    private final UserInfoMapper userInfoMapper;

    @Override
    public Integer userCenterAddFile(UserCenterAddFileReq req) {
        Integer userId = SecurityUtils.getUserId();
        FileEntity entity = new FileEntity();
        entity.setFileName(req.getFileName());
        entity.setIntroduce(req.getIntroduce());
        entity.setFileSuffix(req.getPath().substring(req.getPath().lastIndexOf(".")));
        entity.setUserId(userId);
        entity.setPath(req.getPath());
        entity.setCreateTime(LocalDateTime.now());
        entity.setDownloadCount(0);
        this.getBaseMapper().insert(entity);
        return entity.getId();
    }

    @Override
    public Page<UserCenterFilePageRes> userCenterFilePage(BasePageReq req) {
        Page page = new Page(req.getPageNum(),req.getPageSize());
        Integer userId = SecurityUtils.getUserId();
        Page<UserCenterFilePageRes> res = this.getBaseMapper().userCenterFilePage(userId,page);
        return res;
    }

    @Override
    public void userCenterUpdateFile(UserCenterUpdateFileReq req) {
        FileEntity entity = this.getBaseMapper().selectById(req.getId());
        if(entity==null){
            throw new BusinessException("文件记录不存在");
        }
        if(req.getFileName()!=null && req.getFileName()!=""){
            entity.setFileName(req.getFileName());
        }
        if(req.getIntroduce()!=null && req.getIntroduce()!=""){
            entity.setIntroduce(req.getIntroduce());
        }
        if(req.getPath()!=null && req.getPath()!=""){
            entity.setPath(req.getPath());
            entity.setFileSuffix(req.getPath().substring(req.getPath().lastIndexOf(".")));
        }
        this.saveOrUpdate(entity);
    }

    @Override
    public void userCenterDeleteFile(UserCenterDeleteFileReq req) {
        this.removeById(req.getId());
    }

    @Override
    public Page<FilePageRes> filePage(FilePageReq req) {
        Page page = new Page(req.getPageNum(),req.getPageSize());
        Page<FilePageRes> res = this.getBaseMapper().pageFile(req,page);
        return res;
    }

    @Override
    public FileDetailRes fileDetail(Integer fileId) {
        FileEntity entity = this.getBaseMapper().selectById(fileId);
        if(entity==null){
            throw new BusinessException("文件记录不存在");
        }
        FileDetailRes res = new FileDetailRes();
        BeanUtils.copyProperties(entity,res);
        UserInfoEntity user = userInfoMapper.selectById(entity.getUserId());
        res.setUserName(user.getName());
        return res;
    }

    @Override
    public void deleteFile(DeleteFileReq req) {
        this.removeById(req.getId());
    }

    @Override
    public Page<UserPageFileRes> userPageFile(UserPageFileReq req) {
        Page page = new Page(req.getPageNum(),req.getPageSize());
        Page<UserPageFileRes> res = this.getBaseMapper().userPageFile(req,page);
        return res;
    }

    @Override
    public UserFileDetailRes userFileDetail(Integer fileId) {
        UserFileDetailRes res = new UserFileDetailRes();
        FileEntity fileEntity = this.getById(fileId);
        UserInfoEntity user = userInfoMapper.selectById(fileEntity.getUserId());
        BeanUtils.copyProperties(fileEntity,res);
        res.setUserName(user.getName());
        return res;
    }

    @Override
    public void downloadCount(String path) {
        QueryWrapper<FileEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FileEntity::getPath,path);
        FileEntity fileEntity = this.getOne(wrapper);
        if(fileEntity==null){
            return;
        }
        else {
            fileEntity.setDownloadCount(fileEntity.getDownloadCount()+1);
        }
        saveOrUpdate(fileEntity);
    }
}
